<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="120px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="24" class="mb20">
					<el-form-item label="归属单位" prop="deptId">
						<el-tree-select
							v-model="form.deptId"
							:data="parentData"
							:props="{ value: 'id', label: 'name', children: 'children', disabled: 'isLock' }"
							clearable
							filterable
							check-strictly
							:disabled="!!form.id"
							:render-after-expand="false"
							placeholder="请选择单位"
							ref="treeRef"
						/>
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="地图标记" prop="longitude">
						<el-button type="primary" icon="location" @click="AMapDialogRef.openDialog(form)">精确选择</el-button>
						<span class="ml-1">{{ form.longitude }}</span>
						<span class="ml-2">{{ form.latitude }}</span>
					</el-form-item>
				</el-col>
				<el-col :span="24" class="mb20">
					<el-form-item label="建筑物名称" prop="buildingName">
						<el-input v-model="form.buildingName" placeholder="请输入建筑物名称" />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="详细地址" prop="detailAddress">
						<el-input v-model="form.detailAddress" placeholder="请输入详细地址" />
					</el-form-item>
				</el-col>
				<el-col :span="16" class="mb20">
					<el-form-item label="新增楼层数" prop="boundFloorCount">
						<div class="flex items-center w-full">
							<el-input-number
								type="number"
								v-model="form.boundFloorCount"
								:disabled="!!form.id"
								:min="0"
								:max="999"
								:value-on-clear="0"
								:precision="0"
								:step="1"
								:step-strictly="true"
								placeholder="请输入新增楼层数"
								class="mr-2"
							/>
							<el-tooltip effect="dark" content="建筑物创建时生成楼层数量，可减少录入楼层工作量，范围0-999" placement="top">
								<el-icon><QuestionFilled /></el-icon>
							</el-tooltip>
						</div>
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="备注" prop="remarks">
						<el-input v-model="form.remarks" placeholder="请输入备注" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
	<AMapDialog ref="AMapDialogRef" @confirm="handleAMapConfirm" />
</template>

<script setup lang="ts" name="BasicPlaceBuildingDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from '/@/hooks/message';
import { getObj, addObj, putObj } from '/@/api/installation/basicPlaceBuilding';
import { rule } from '/@/utils/validate';
import { deptTree } from '/@/api/admin/dept';
const AMapDialog = defineAsyncComponent(() => import('./components/AMap.vue'));
const emit = defineEmits(['refresh']);

// 定义变量内容
const AMapDialogRef = ref();
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
const parentData = ref([]);
// 定义字典

// 提交表单数据
const form = reactive({
	id: '',
	buildingName: '',
	deptId: '',
	longitude: '',
	latitude: '',
	detailAddress: '',
	boundFloorCount: 0,
	remarks: '',
});

// 定义校验规则
const dataRules = ref({
	deptId: [{ required: true, message: '单位id不能为空', trigger: 'blur' }],
	longitude: [{ required: true, message: '请选择地图标记', trigger: 'blur' }],
	buildingName: [{ required: true, message: '建筑物名称不能为空', trigger: 'blur' }],
	detailAddress: [{ required: true, message: '详细地址不能为空', trigger: 'blur' }],
});

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.id = '';
	form.latitude = '';
	form.longitude = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

	// 获取basicPlaceBuilding信息
	if (id) {
		form.id = id;
		getbasicPlaceBuildingData(id);
	}
	getDeptData();
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getbasicPlaceBuildingData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj(id)
		.then((res: any) => {
			Object.assign(form, res.data);
		})
		.finally(() => {
			loading.value = false;
		});
};

const getDeptData = async () => {
	deptTree().then((res) => {
		parentData.value = res.data;
	});
};

const handleAMapConfirm = (data: any) => {
	const { lat, lng, address, building } = data;
	form.latitude = lat;
	form.longitude = lng;
	form.detailAddress = address;
	form.buildingName = building;
};
// 暴露变量
defineExpose({
	openDialog,
});
</script>
